package cn.cdut.servlet.tools.JDBC;

import java.sql.*;

public class tools {
   private static String rname="root";
   private static String rpassword="123456";
   private static String url="jdbc:mysql://localhost:3306/gui?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
   private static Connection connection=null;
   private static PreparedStatement preparedStatement=null;
   static {
       try {
           Class.forName("com.mysql.cj.jdbc.Driver");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
       try {
          connection = DriverManager.getConnection(url, rname,rpassword);
       } catch (SQLException throwables) {
           throwables.printStackTrace();
       }

   }
   public static boolean find(String email) throws SQLException {
       String sql="select * from denglu where email=?";
       preparedStatement = connection.prepareStatement(sql);
       preparedStatement.setString(1,email);
       try {
           ResultSet resultSet = preparedStatement.executeQuery();
           int cnt=0;
           while (resultSet.next()){
               cnt++;
           }
           if(cnt==1)return true;
           else return false;
       } catch (SQLException throwables) {
           throwables.printStackTrace();
           return false;
       }
   }
    public static boolean find(String email ,String password) throws SQLException {
        String sql="select * from denglu where email=?and password=?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,email);
        preparedStatement.setString(2,password);
        ResultSet re=preparedStatement.executeQuery();
        int cnt=0;
        while(re.next()){
            cnt++;
        }
        if(cnt==1)return true;
        else return false;
    }
   public static void insert(String email,String password) throws Exception {
       String sql="insert into denglu (email, password) values(?,?);";
       preparedStatement = connection.prepareStatement(sql);
       preparedStatement.setString(1,email);
       preparedStatement.setString(2,password);
       preparedStatement.executeUpdate();
   }
   public static void colse(){
       try {
           preparedStatement.close();
       } catch (SQLException throwables) {
           throwables.printStackTrace();
       }
       try {
           connection.close();
       } catch (SQLException throwables) {
           throwables.printStackTrace();
       }
   }
}
